package com.library.service;

import com.library.bean.ReaderCard;
import com.library.constant.CommonConstant;
import com.library.entity.OaApprove;
import com.library.entity.OaApproveExt;
import com.library.entity.OaAttendanceRecord;
import com.library.entity.OaAttendanceRecordExt;
import com.library.mapper.OaApproveMapper;
import com.library.mapper.OaAttendanceRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ido
 * @since 2024-12-14
 */

@Service
public class OaApproveServiceImpl {
    private final static Logger log = LoggerFactory.getLogger(LoginService.class);

    @Autowired
    private OaApproveMapper oaApproveMapper;
    @Autowired
    private OaAttendanceRecordMapper oaAttendanceRecordMapper;

    public List<OaApproveExt> list(OaApprove userInfo) {
        ReaderCard loginReaderCard = CommonUtils.getLoginReaderCard();
        String loginReaderCardRole = CommonUtils.getLoginReaderCardRole();
        if(!StringUtils.isEmpty(loginReaderCardRole)){
            OaApproveExt oaApproveExt = new OaApproveExt();
            oaApproveExt.setUserInfoId(loginReaderCard.getReaderId());
            if(CommonConstant.role_manager.equalsIgnoreCase(loginReaderCardRole)){
                oaApproveExt.setStatus(CommonConstant.approve_manager);
            }else if(CommonConstant.role_general_manager.equalsIgnoreCase(loginReaderCardRole)){
                oaApproveExt.setStatus(CommonConstant.approve_gen_manager);
            }
            List<OaApproveExt> oaApproveExts = this.oaApproveMapper.selectByQuery(oaApproveExt);
            for (OaApproveExt approveExt : oaApproveExts) {
                if (CommonConstant.approve_don.equals(approveExt.getStatus())) {
                    approveExt.setStatusName("审批通过");
                } else if (CommonConstant.approve_reject.equals(approveExt.getStatus())) {
                    approveExt.setStatusName("审批驳回");
                } else if (CommonConstant.approve_manager.equals(approveExt.getStatus())) {
                    approveExt.setStatusName("待部门经理审批");
                }if (CommonConstant.approve_gen_manager.equals(approveExt.getStatus())){
                    approveExt.setStatusName("待总经理审批");
                }
            }
            return oaApproveExts;
        }
        return null;
    }

    public void add(OaApprove oaApprove) {

        Date recordDate = oaApprove.getRecordDate();
        Calendar calendar = CommonUtils.getCalendarYYYYMMDD(recordDate);
        // 获取修改后的时间
        Date yyyymmdd = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8")); // 设置为GMT+8时区
        Date now = new Date();
        log.info("Current time in GMT+8: 当前时间" + sdf.format(now));
        log.info("Current time in GMT+8:  打卡时间 " + sdf.format(recordDate));
        log.info("Current time in GMT+8:  打卡日期 " + sdf.format(yyyymmdd));
        OaApprove record = new OaApprove();
        record.setRecordDate(yyyymmdd);
        ReaderCard loginReaderCard = CommonUtils.getLoginReaderCard();
        String loginReaderCardRole = CommonUtils.getLoginReaderCardRole();
        if (!StringUtils.isEmpty(loginReaderCardRole)) {
            if (CommonConstant.role_manager.equalsIgnoreCase(loginReaderCardRole)) {
                record.setStatus(CommonConstant.approve_gen_manager);
            } else if (CommonConstant.role_general_manager.equalsIgnoreCase(loginReaderCardRole)) {
                record.setStatus(CommonConstant.approve_don);
            } else if (CommonConstant.role_staff.equalsIgnoreCase(loginReaderCardRole)) {
                record.setStatus(CommonConstant.approve_manager);
            }
            record.setRemark(oaApprove.getRemark());
            record.setUserInfoId(loginReaderCard.getReaderId());
            int insert = this.oaApproveMapper.insert(record);
        }
    }

    public void delete(OaApprove userInfo) {

    }

//    public void update(OaApprove userInfo) {
//
//    }
//
//    public OaApprove detail(OaApprove userInfo) {
//        return null;
//    }

    @Transactional(rollbackFor = Exception.class)
    public void approve(OaApprove oaApprove1) {
        Long id = oaApprove1.getId();
        OaApprove oaApprove = this.oaApproveMapper.selectByPrimaryKey(id);
        String loginReaderCardRole = CommonUtils.getLoginReaderCardRole();
        if (loginReaderCardRole != null) {
            OaApprove record = new OaApprove();
            record.setId(id);
            if (CommonConstant.role_manager.equalsIgnoreCase(loginReaderCardRole)) {
                record.setStatus(CommonConstant.approve_gen_manager);
            } else if (CommonConstant.role_general_manager.equalsIgnoreCase(loginReaderCardRole)) {
                //更新打卡记录为请假
                OaAttendanceRecordExt query = new OaAttendanceRecordExt();
                query.setUserInfoId(oaApprove.getUserInfoId());
                query.setRecordDate(oaApprove.getRecordDate());
                List<OaAttendanceRecordExt> oaAttendanceRecordExts = this.oaAttendanceRecordMapper.listByQuery(query);
                for (OaAttendanceRecordExt oaAttendanceRecordExt : oaAttendanceRecordExts) {
                    OaAttendanceRecord record1 = new OaAttendanceRecord();
                    record1.setId(oaAttendanceRecordExt.getId());
                    record1.setStatus(CommonConstant.recordStatus_qingjia);
                    // 更新为请假状态
                    int i = this.oaAttendanceRecordMapper.updateByPrimaryKeySelective(record1);
                }
                record.setStatus(CommonConstant.approve_don);
            } else if (CommonConstant.role_staff.equalsIgnoreCase(loginReaderCardRole)) {
                record.setStatus(CommonConstant.approve_manager);
            }
            int insert = this.oaApproveMapper.updateByPrimaryKeySelective(record);
        }
    }

    public void reject(OaApprove userInfo) {
        Long id = userInfo.getId();
        OaApprove oaApprove = this.oaApproveMapper.selectByPrimaryKey(id);
        String loginReaderCardRole = CommonUtils.getLoginReaderCardRole();
        if (loginReaderCardRole != null) {
            OaApprove record = new OaApprove();
            record.setId(id);
            record.setStatus(CommonConstant.approve_reject);
            int insert = this.oaApproveMapper.updateByPrimaryKeySelective(record);
        }
    }
}
